import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

//=======================  Utils  ===========================
import {initMenu} from "@/network/menu";

//=======================  UI/插件 ===========================
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import "font-awesome/css/font-awesome.css";
import {getUser} from "@/api/user";

//=======================  Vue引用  ===========================
Vue.use(ElementUI, {size: "small"});

//=======================  全局变量  ===========================
Vue.config.productionTip = false;

//=======================  路由守卫  ===========================

router.beforeEach(((to, from, next) => {
    if (window.sessionStorage.getItem('token')) {
        if (router.options.routes[2].children.length <= 1) {
            initMenu(router, store);
        }
        if (!window.sessionStorage.getItem('userInfo')) {
            getUser(window.sessionStorage.getItem("token"))
                .then(res => {
                    store.dispatch('setUserInfo', res.data)
                });
        }
        next();
    } else {
        if (to.path === '/login') {
            next();
        } else {
            next('/login');
        }
    }
}));

//=======================  Vue  ===========================
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')